﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Finance
{
    public class ShopCate_ListController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Finance/shopcate_list.cshtml";
        public IShop_categoryService _shop_category { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ActionResult result = View(EDIT_RESULT_VIEW);
        }
        // GET: /admin/Category_List/

        public ActionResult Index()
        {
            ChkAdminLevel("shop_category", CMSEnums.ActionEnum.View.ToString()); //检查权限
            RptBind();
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ChkAdminLevel("shop_category", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken list = jobject["list"];
            foreach (JToken item in list)
            {
                int id = int.Parse(item["id"].ToString());
                int sortId = int.Parse(item["sortId"].ToString());
                _shop_category.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "保存栏目分类排序"); //记录日志
            string script = JscriptMsg("保存排序成功！", Utils.CombUrlTxt("index",""));
            return Content(script);
        }

        [HttpPost]
        public ActionResult SubmitDelete()
        {
            ChkAdminLevel("shop_category", CMSEnums.ActionEnum.Delete.ToString()); //检查权限
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken list = jobject["list"];
            foreach (JToken item in list)
            {
                int id = int.Parse(item["id"].ToString());
                _shop_category.Delete(id);
            }
            AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "删除栏目分类数据"); //记录日志
            string script = JscriptMsg("删除数据成功！", Utils.CombUrlTxt("index", ""));
            return Content(script);
        }

        //数据绑定
        private void RptBind()
        {
            ViewData["list"] = _shop_category.GetList(0);
        }
    }
}
